//
//  UIWebView+Hanck.m
//  XCYDSJ
//
//  Created by 黄杰 on 15/10/8.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "UIWebView+Hanck.h"
#import <objc/runtime.h>

@interface UIWebView ()
@end

@implementation UIWebView (Hanck)

- (UIView *)browserView
{
    UIScrollView *scrollView = self.scrollView;
    
    UIView *browserView = nil;
    for (UIView *subview in scrollView.subviews) {
        if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
            browserView = subview;
            break;
        }
    }
    
    return browserView;
}

- (void)replaceInputAccessoryView
{
    // 找到UIWebBrowserView的对象
    UIView *browserView = [self browserView];
    
    // 拷贝 browserView 中的UIWebBrowserViewMinusAccessoryView
    Class newClass = objc_duplicateClass([browserView class], "UIWebBrowserViewMinusAccessoryView", 0);
    
    // 编写自己的实现方法accessoryViewImp
    IMP accessoryViewImp = [self methodForSelector:@selector(emptyInputAccessoryView)];
    
    // 替换系统的inputAccessoryView方法 ，@@: 是type coding
    class_replaceMethod(newClass, @selector(inputAccessoryView), accessoryViewImp, "@@:");
    
    // 注册类
    objc_registerClassPair(newClass);
    
    // 重新设计browserView对象的类
    object_setClass(browserView, newClass);
    
    // 载入自己的inputAccessoryView
    [browserView reloadInputViews];
}

- (id)emptyInputAccessoryView
{
    UIView *view = [[UIView alloc] init];
    return view;
}

@end
